package com.ymatou.iapi.im.testcase;

import com.google.gson.JsonObject;
import com.ymatou.iapi.im.parameter.AddMessageAttachBean;
import com.ymatou.iapi.im.parameter.SendMessageBean;
import com.ymatou.iapi.im.parameter.SendMessageToBuyerBean;
import com.ymatou.iapi.im.parameter.SendMessageToSellerBean;
import com.ymatou.iapi.im.parameter.emu.MessageAttachType;
import com.ymatou.iapi.im.service.AddMessageAttachCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.model.YmtMessageAttach;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

/**
*添加消息附件
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AddMessageAttach{
	private static AddMessageAttachBean addmessageattachBean;
	private static AddMessageAttachCall addmessageattachCall;
	private static int imId = Integer.parseInt(EnvSetup.getData("userId_IM"));
	private static String imName = EnvSetup.getData("userName_IM");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加消息附件");
	}

	@Before
	public void caseUp() {
		addmessageattachBean=new AddMessageAttachBean();
		addmessageattachCall=new AddMessageAttachCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addmessageattachCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_001() {
		Logger.start(true,"添加消息附件,买家给买手发送消息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			ImDefaultWapper im = new ImDefaultWapper();
			int ToUserId = 246224;
			String message = "我是helloworld";
			SendMessageToSellerBean sellerBean = ImCallService.initSendMessageToSellerBean(userId,ToUserId,message);
			int FromUserId = sellerBean.getFromUserId();
			JsonObject jet = ImCallService.SendMessageToSeller(sellerBean);

			addmessageattachBean.setAppId(sellerBean.getAppId());
			String content = "Chinese people lovely";
			addmessageattachBean.setContent(content);
			String messageId = jet.get("messageId").getAsString();
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			YmtMessageAttach map = im.selectMessageattachByMessageId(messageId).get(0);
			Logger.verifyEquals("Success",addmessageattachCall.getString("Status"),"验证Status");
			Logger.comment("验证messageattach数据库的准确性");
			String[] messageIdArr = messageId.split("_");
			long id = Long.parseLong(messageIdArr[2]);
			Logger.verifyEquals(id,map.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,map.getType(),"验证Type");
			Logger.verifyEquals(content,map.getContent(),"验证Content");
			Logger.comment("验证message数据库数据的准确性");
			YmtMessage m = im.selectImInfo(ToUserId,FromUserId,Integer.parseInt(messageIdArr[2])).get(0);
			Logger.debug(m);
			Logger.verifyEquals(id,m.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,m.getHaveAttach(),"验证MessageId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_002() {
		Logger.start(true,"添加消息附件,买手给买家发送消息");
		try {
			ImDefaultWapper im = new ImDefaultWapper();
			int fromUserId = 24635724;
			int ToUserId = 246224;
			String message = "我是helloworld";

			SendMessageToBuyerBean buyerBean = ImCallService.initSendMessageToBuyerBean(fromUserId,ToUserId,message);
			JsonObject jet = ImCallService.SendMessageToBuyer(buyerBean);

			addmessageattachBean.setAppId(buyerBean.getAppId());
			String content = "Chinese people lovely";
			addmessageattachBean.setContent(content);
			String messageId = jet.get("messageId").getAsString();
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			YmtMessageAttach map = im.selectMessageattachByMessageId(messageId).get(0);
			Logger.verifyEquals("Success",addmessageattachCall.getString("Status"),"验证Status");
			Logger.comment("验证messageattach数据库的准确性");
			String[] messageIdArr = messageId.split("_");
			long id = Long.parseLong(messageIdArr[2]);
			Logger.verifyEquals(id,map.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,map.getType(),"验证Type");
			Logger.verifyEquals(content,map.getContent(),"验证Content");
			Logger.comment("验证message数据库数据的准确性");
			YmtMessage m = im.selectImInfo(ToUserId,fromUserId,Integer.parseInt(messageIdArr[2])).get(0);
			Logger.debug(m);
			Logger.verifyEquals(id,m.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,m.getHaveAttach(),"验证MessageId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_003() {
		Logger.start(true,"添加消息附件,洋管家发送消息");
		try {
			ImDefaultWapper im = new ImDefaultWapper();
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();

			SendMessageBean sendMessageBean = ImCallService.initSendMessage(imId,userId,imName);
			JsonObject jet = ImCallService.SendMessage(sendMessageBean);

			String content = "Chinese people lovely";
			addmessageattachBean.setContent(content);
			addmessageattachBean.setAppId("app.ymatou.com");
			String messageId = jet.get("messageId").getAsString();
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			YmtMessageAttach map = im.selectMessageattachByMessageId(messageId).get(0);
			Logger.verifyEquals("Success",addmessageattachCall.getString("Status"),"验证Status");
			Logger.comment("验证messageattach数据库的准确性");
			String[] messageIdArr = messageId.split("_");
			long id = Long.parseLong(messageIdArr[2]);
			Logger.verifyEquals(id,map.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,map.getType(),"验证Type");
			Logger.verifyEquals(content,map.getContent(),"验证Content");
			Logger.comment("验证message数据库数据的准确性");
			YmtMessage m = im.selectImInfo(imId,userId,Integer.parseInt(messageIdArr[2])).get(0);
			Logger.debug(m);
			Logger.verifyEquals(id,m.getMessageId(),"验证MessageId");
			Logger.verifyEquals(1,m.getHaveAttach(),"验证MessageId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_004() {
		Logger.start(true,"添加消息附件,不传appid");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();

			SendMessageBean sendMessageBean = ImCallService.initSendMessage(imId,userId,imName);
			JsonObject jet = ImCallService.SendMessage(sendMessageBean);

			String content = "Chinese people lovely";
			addmessageattachBean.setContent(content);
			String messageId = jet.get("messageId").getAsString();
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			Logger.verifyEquals("BussinessError",addmessageattachCall.getString("Status"),"验证Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_005() {
		Logger.start(true,"添加消息附件,错误的MessageId");
		try {
			addmessageattachBean.setAppId("app.ymatou.com");
			String content = "Chinese people lovely";
			addmessageattachBean.setContent(content);
			String messageId = "123dshji";
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			Logger.verifyEquals("ParameterError",addmessageattachCall.getString("Status"),"验证Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddMessageAttach_006() {
		Logger.start(true,"添加消息附件,不传Content");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();

			SendMessageBean sendMessageBean = ImCallService.initSendMessage(imId,userId,imName);
			JsonObject jet = ImCallService.SendMessage(sendMessageBean);

			addmessageattachBean.setAppId("app.ymatou.com");
			String messageId = jet.get("messageId").getAsString();
			addmessageattachBean.setMessageId(messageId);
			addmessageattachBean.setType(MessageAttachType.AppOrder);
			addmessageattachCall.setData(addmessageattachBean);
			addmessageattachCall.callService();
			Logger.verifyEquals("BussinessError",addmessageattachCall.getString("Status"),"验证Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}